﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using NNS.UI;

namespace QLTB.ThietBi.UI
{
    public partial class frmThietBi : XtraForm
    {
        private readonly ucThietBi uc = new ucThietBi();

        public frmThietBi()
        {
            InitializeComponent();
            uc.Dock = DockStyle.Fill;
            Controls.Add(uc);
            uc.SaveComplete += uc_SaveComplete;
            uc.SaveChanging += uc_SaveChanging;
            uc.SaveChanged += uc_SaveChanged;
        }

        private frmMain MyRibbon
        {
            get { return ParentForm as frmMain; }
        }

        private void uc_SaveChanged(object sender, SaveChangedEventArgs e)
        {
            if (e.NotSave)
            {
                MyRibbon.iSave.ImageIndex = 69;
                Text = "Quản lý thiết bị (*)";
                //this.Appearance.ForeColor = Color.Red;
            }
            else
            {
                MyRibbon.iSave.ImageIndex = 24;
                Text = "Quản lý thiết bị";
            }
        }

        private void uc_SaveChanging(object sender, SaveChangingEventArgs e)
        {
            MyRibbon.SetValue(e.Percent);
        }

        private void uc_SaveComplete(object sender, SaveCompleteEventArgs e)
        {
            MyRibbon.ShowProcess = BarItemVisibility.Never;
            if (e != null) MyRibbon.Message = string.Format("{0:##,##0.00 giây}", e.Item);
        }

        private void frmThietBi_Load(object sender, EventArgs e)
        {
            NapInfo();
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (MyRibbon != null)
            {
                MyRibbon.MyRibbon.SetPopupContextMenu(this, MyRibbon.pm);
            }
        }

        private void RemoveEvent()
        {
            MyRibbon.iFilter.Checked = uc.IsFilter;
            MyRibbon.iFilter.ItemClick -= iFilter_ItemClick;
            MyRibbon.iClose.ItemClick -= iClose_ItemClick;
            MyRibbon.iSave.ItemClick -= iSave_ItemClick;
            MyRibbon.iRefesh.ItemClick -= iRefesh_ItemClick;
            MyRibbon.iCopy.ItemClick -= iCopy_ItemClick;
            MyRibbon.iSelectAll.ItemClick -= iSelectAll_ItemClick;
            MyRibbon.iClear.ItemClick -= iClear_ItemClick;
            MyRibbon.iDelete.ItemClick -= iDelete_ItemClick;
            MyRibbon.iDuplicate.ItemClick -= iDuplicate_ItemClick;
            MyRibbon.iComponents.ItemClick -= iComponents_ItemClick;
            MyRibbon.iEquipHistory.ItemClick -= iEquipHistory_ItemClick;
            // MyRibbon.INext.ItemClick -= INext_ItemClick;
            // MyRibbon.IPrevious.ItemClick -= IPrevious_ItemClick;
        }

        private void AddEvent()
        {
            MyRibbon.iFilter.Checked = uc.IsFilter;
            MyRibbon.iFilter.ItemClick += iFilter_ItemClick;
            MyRibbon.iClose.ItemClick += iClose_ItemClick;
            MyRibbon.iSave.ItemClick += iSave_ItemClick;
            MyRibbon.iCancel.ItemClick += iCancel_ItemClick;
            MyRibbon.iRefesh.ItemClick += iRefesh_ItemClick;
            MyRibbon.iCopy.ItemClick += iCopy_ItemClick;
            MyRibbon.iSelectAll.ItemClick += iSelectAll_ItemClick;
            MyRibbon.iClear.ItemClick += iClear_ItemClick;
            MyRibbon.iDelete.ItemClick += iDelete_ItemClick;
            MyRibbon.iDuplicate.ItemClick += iDuplicate_ItemClick;
            MyRibbon.iComponents.ItemClick += iComponents_ItemClick;
            MyRibbon.iEquipHistory.ItemClick += iEquipHistory_ItemClick;
            // MyRibbon.INext.ItemClick += INext_ItemClick;
            // MyRibbon.IPrevious.ItemClick += IPrevious_ItemClick;
        }

        private void IPrevious_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Previous();
        }

        private void INext_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Next();
        }

        private void iDuplicate_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Duplicate();
        }

        private void iFilter_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.IsFilter = MyRibbon.iFilter.Checked;
        }

        private void iEquipHistory_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.EquipHistory();
        }

        private void iComponents_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Components();
        }

        private void iDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            Delete();
        }

        private void iClear_ItemClick(object sender, ItemClickEventArgs e)
        {
            Clear();
        }

        private void iSelectAll_ItemClick(object sender, ItemClickEventArgs e)
        {
            SelectAll();
        }

        private void iCopy_ItemClick(object sender, ItemClickEventArgs e)
        {
            Copy();
        }

        private void iRefesh_ItemClick(object sender, ItemClickEventArgs e)
        {
            Nap();
        }

        private void iCancel_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cancel();
        }

        private void iSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            Save();
        }

        private void iClose_ItemClick(object sender, ItemClickEventArgs e)
        {
            Close();
        }

        public void NapInfo()
        {
            uc.NapInfo();
        }

        public void Nap()
        {
            uc.Nap();
        }

        public void SelectAll()
        {
            uc.SelectAll();
        }

        public void Copy()
        {
            uc.Copy();
        }

        public void Add()
        {
            uc.ClearContext();
        }

        public void Cancel()
        {
            uc.Cancel();
        }

        public void Save()
        {
            uc.Save();
        }

        public void Delete()
        {
            uc.Delete();
        }

        public void Clear()
        {
            uc.Clear();
        }

        public void Previous()
        {
            uc.Previous();
        }

        public void Next()
        {
            uc.Next();
        }

        private void frmThietBi_Activated(object sender, EventArgs e)
        {
            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = true;
                MyRibbon.iEdit.Visible = true;
                MyRibbon.igPrint.Visible = false;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Always;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Always;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Always;
                MyRibbon.iSave.Visibility = BarItemVisibility.Always;
                MyRibbon.iClose.Visibility = BarItemVisibility.Always;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Always;
                // MyRibbon.iNavigate.Visible = true;
                MyRibbon.iComponents.Visibility = BarItemVisibility.Always;
                MyRibbon.iEquipHistory.Visibility = BarItemVisibility.Always;
                //MyRibbon.iEquipmentTransfer.Visibility = BarItemVisibility.Always;

                // MyRibbon.IPrevious.Visibility = BarItemVisibility.Always;
                // MyRibbon.INext.Visibility = BarItemVisibility.Always;
                MyRibbon.iFilter.Visibility = BarItemVisibility.Always;
                MyRibbon.iDuplicate.Visibility = BarItemVisibility.Always;

                MyRibbon.iCopy.Visibility = BarItemVisibility.Always;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Always;
                MyRibbon.iClear.Visibility = BarItemVisibility.Always;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Always;

                AddEvent();
            }

            NapInfo();
        }

        private void frmThietBi_Deactivate(object sender, EventArgs e)
        {
            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = false;
                MyRibbon.iEdit.Visible = false;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Never;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Never;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.igPrint.Visible = false;
                MyRibbon.iSave.Visibility = BarItemVisibility.Never;
                MyRibbon.iClose.Visibility = BarItemVisibility.Never;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Never;
                //MyRibbon.iNavigate.Visible = false;
                MyRibbon.iComponents.Visibility = BarItemVisibility.Never;
                MyRibbon.iEquipHistory.Visibility = BarItemVisibility.Never;
                //MyRibbon.iEquipmentTransfer.Visibility = BarItemVisibility.Never;
                //MyRibbon.IPrevious.Visibility = BarItemVisibility.Never;
                //MyRibbon.INext.Visibility = BarItemVisibility.Never;
                MyRibbon.iFilter.Visibility = BarItemVisibility.Never;
                MyRibbon.iDuplicate.Visibility = BarItemVisibility.Never;

                MyRibbon.iCopy.Visibility = BarItemVisibility.Never;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Never;
                MyRibbon.iClear.Visibility = BarItemVisibility.Never;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Never;

                RemoveEvent();
            }
        }

        private void frmThietBi_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (uc.NotSave)
            {
                DialogResult dialogResult = MyBox.ShowSave("Dữ liệu chưa được lưu!\nBạn có muốn lưu lại không?",
                                                           "Quản lý thiết bị");

                if (dialogResult == DialogResult.Cancel)
                {
                    e.Cancel = true;
                    return;
                }
                if (dialogResult == DialogResult.Yes)
                {
                    Save();
                }
            }

            Hide();
            e.Cancel = !Program._exit;
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F5:
                    NapInfo();
                    return true;

                case Keys.Control | Keys.S:
                    Save();
                    return true;
                case Keys.Control | Keys.W:
                    Save();
                    return true;
                case Keys.F2:
                    Save();
                    return true;
                case Keys.Alt | Keys.X:
                    Close();
                    return true;

                case Keys.F10:

                    Close();
                    return true;
            }

            return false;
        }
    }
}